package com.twu.biblioteca.transaction;

import com.twu.biblioteca.item.Item;
import com.twu.biblioteca.item.Items;
import com.twu.biblioteca.user.User;

/**
 * Created by Enrico on 18/09/2014.
 */
public class Transaction {

    private Items<? extends Item> items;

    public Transaction(Items<? extends Item> items) {
        this.items = items;
    }

    public void doCheckout(String title, User user) {
        Item item = lookup(title);

        if (item != null){
            //item was found
            item.showCheckoutMessage();
            item.doCheckout(user);
        }else{
            showItemNotFound();
        }
    }

    public void doReturn(String title, User user) {
        Item item = lookup(title);

        if (item != null) {
            //item was found
            item.showReturnMessage();
            item.doReturn(user);
        }else{
            showItemNotFound();
        }
    }

    public void showItemNotFound(){
        System.out.println("The item does not exist.");
    }

    public void listItems(){
        items.list();
    }

    public Item lookup(String title){
        return items.lookup(title);
    }
}
